/**
 * 基数排序
 *
 * @param {number[]} nums
 * @return {number[]}
 */
function radixSort(nums) {
  const length = nums.length;
  if (length < 2) {
    return nums;
  }
  let max = nums[0];
  for (let i = 1; i < length; i++) {
    max = Math.max(max, nums[i]);
  }
  let radix = 1;
  while (Math.pow(10, radix) < max) {
    radix++;
  }
  let list;
  for (let i = 0; i < radix; i++) {
    list = [];
    for (let j = 0; j < 10; j++) {
      list.push([]);
    }
    for (let j = 0; j < length; j++) {
      list[Math.floor((nums[j] % Math.pow(10, i + 1)) / Math.pow(10, i))].push(
        nums[j]
      );
    }
    nums = [].concat(...list);
  }
  return [].concat(...list);
}

const nums = [3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48];
const nums2 = [3, 44, 38, 5, 427, 15, 36, 26, 27, 2, 1246, 4, 19, 50, 48];
console.log(radixSort(nums));
console.log(radixSort(nums2));
